#include <TFT_eSPI.h>
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <DNSServer.h>

const char* ssid = "凑有希那";
const char* password = "100861001";

const byte DNS_PORT = 53;
IPAddress apIP(192, 168, 0, 101);
DNSServer dnsServer;

TFT_eSPI tft;




void setup() {
    tft.init();//TFT——LCD初始化
    tft.setRotation(7);
    tft.fillScreen(TFT_BLACK);//设置屏幕底色为黑色
    tft.setTextColor(TFT_GREEN);//设置文本为绿色
    Serial.begin(115200);
    WiFi.mode(WIFI_AP_STA);
    WiFi.begin(ssid, password);
    Init();
    
  dnsServer.start(DNS_PORT, "*", apIP);


    }


void loop() {
 dnsServer.processNextRequest();
 mainFun();
}

void Init(){
    tft.drawCentreString("Connecting", 75, 50, 2);
    Serial.print("Connecting to ");
    Serial.println(ssid);
    while (WiFi.status() != WL_CONNECTED) {
      delay(250);
      tft.fillScreen(TFT_BLACK);
      tft.drawCentreString("Connecting.", 75, 50, 2);
      tft.drawCentreString("|",125, 50, 2);
      delay(250);
      tft.fillScreen(TFT_BLACK);
      tft.drawCentreString("Connecting..", 75, 50, 2);
      tft.drawCentreString("/",125, 50, 2);
      delay(250);
      tft.fillScreen(TFT_BLACK);
      tft.drawCentreString("Connecting...", 75, 50, 2);
      tft.drawCentreString("---",125, 50, 2);
      delay(250);
      tft.fillScreen(TFT_BLACK);
      tft.drawCentreString("Connecting....", 75, 50, 2);
      tft.drawCentreString("\\",125, 50, 2);
      delay(250);
      Serial.print(".");
    }
    
    Serial.println("");
    Serial.println("Connected to the existing Wi-Fi network");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
    WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
    WiFi.softAP("实验性网络");
 


}

void mainFun(){
  tft.fillScreen(TFT_BLACK);//黑色填充屏幕刷新
// 获取当前连接的设备数量
  int DevicesNum = WiFi.softAPgetStationNum();
  char str[10];
  sprintf(str, "%d", DevicesNum);
  const char* Num = str;
    tft.drawCentreString("Devices:", 25, 25, 2);  
    tft.drawCentreString("@SCI", 25, 92, 2);  //显示科学社署名
    tft.drawCentreString(Num, 60, 25, 2);  
    delay(500);

}
